package labox.innovation.gameserver.network.clientpackets;

import labox.innovation.gameserver.model.BlockList;
import labox.innovation.gameserver.model.FuzeWorld;
import labox.innovation.gameserver.model.L2Party;
import labox.innovation.gameserver.model.actor.instance.FuzePcInstance;
import labox.innovation.gameserver.network.SystemMessageId;
import labox.innovation.gameserver.network.serverpackets.AskJoinParty;
import labox.innovation.gameserver.network.serverpackets.SystemMessage;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * @author ljsh
 * @date 2010-4-7
 * @version 1.0
 * @description 邀请加入队伍
 */
public final class RequestJoinParty extends L2GameClientPacket {

	private static final String _C__29_REQUESTJOINPARTY = "[C] 29 RequestJoinParty";

	private static final Logger _log = LoggerFactory.getLogger(RequestJoinParty.class.getName());

	private long targetId;

	private int _itemDistribution;

	/**
	 * 邀请加入自己的队伍
	 */
	private static final int requesttype = 0;

	/**
	 * 申请加入对方队伍
	 */
	private static final int requesttype1 = 1;

	@Override
	protected void readImpl() {
		targetId = readLong();
	}

	@Override
	protected void runImpl() {
		FuzePcInstance requestor = getClient().getActiveChar();
		FuzePcInstance target = FuzeWorld.getInstance().getPlayer(targetId);
		SystemMessage msg = null;
		if (requestor == null)
			return;
		if (target == null) {
			requestor.sendPacket(new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT));// 请求的目标不正确
			return;
		}
		if (target.getAppearance().getInvisible()) {
			requestor.sendPacket(new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT));
			return;
		}
		if (BlockList.isBlocked(target, requestor)) {
			SystemMessage sm = new SystemMessage(SystemMessageId.S1_HAS_ADDED_YOU_TO_IGNORE_LIST);// 你在目标的仇恨列表中
			sm.addCharName(target);
			requestor.sendPacket(sm);
			return;
		}
		if (target == requestor) {
			requestor.sendPacket(new SystemMessage(SystemMessageId.INCORRECT_TARGET));// 不能添加自己如队伍
			return;
		}
		if (target.getRace() != requestor.getRace()) {
			SystemMessage sm = new SystemMessage(SystemMessageId.NO_SAME_RACE_CAN_NOT_IN_SAME_PARTY);// 不是一个种族不能组队
			requestor.sendPacket(sm);
			return;
		}
		if (target.getPunish().isInJail() || requestor.getPunish().isInJail())// 自己或对方在监狱中
		{
			requestor.sendMessage("在监狱中不能组队");
			return;
		}

		if (target.getClient().isDetached())// 目标玩家在离线模式
		{
			requestor.sendMessage("Player is in offline mode.");
			return;
		}
		if (!target.getFriendInstance().getIsInvited()) // 对方关闭了组队邀请
		{
			msg = new SystemMessage(SystemMessageId.TARGET_CLOSE_TEAM_INVITE);
			requestor.sendPacket(msg);
			return;
		}
		if (requestor.isInParty() && target.isInParty()) {
			if (requestor.getParty() == target.getParty()) {
				msg = new SystemMessage(SystemMessageId.YOU_JOINED_S1_PARTY);
				msg.addCharName(requestor.getParty().getLeader());
				requestor.sendPacket(msg);
				return;
			}
			if (requestor.getParty() != target.getParty()) {
				msg = new SystemMessage(SystemMessageId.C1_IS_ALREADY_IN_PARTY);
				msg.addCharName(target);
				requestor.sendPacket(msg);
				return;
			}

		}
		if (!requestor.isInParty() && !target.isInParty()) // 双方都没有队伍，则发送邀请共同创建队伍
		{
			if (target.isAutoAttack) {
				requestor.setParty(new L2Party(requestor));
				requestor.getAI().autoTeam(requestor, target);
				return;
			}
			requestor.onTransactionRequest(target);
			target.sendPacket(new AskJoinParty(requestor.getName(), _itemDistribution, requesttype));
			return;
		}

		if (!requestor.isInParty() && target.isInParty())// 对方已经在队伍中,自己没有队伍，则发送请求加入对方队伍
		{
			addTargetToParty(requestor, target, requesttype1);
			return;
		}

		if (requestor.isInParty() && !target.isInParty()) // Asker is in party
		{
			addTargetToParty(target, requestor, requesttype);
			return;
		}
	}

	/**
	 * @param client
	 * @param itemDistribution
	 * @param target
	 * @param requestor
	 * @param type
	 *            0邀请对方加入自己的队伍 ,1加入对方队伍
	 */
	private void addTargetToParty(FuzePcInstance target, FuzePcInstance requestor, int type) {
		SystemMessage msg;
		L2Party party = null;
		if (type == requesttype) {
			party = requestor.getParty();
			if (!party.isLeader(requestor)) {
				requestor.sendPacket(new SystemMessage(SystemMessageId.ONLY_LEADER_CAN_INVITE));// 只有队长才能邀请其他角色加入队伍
				return;
			}
			if (party.getMemberCount() >= L2Party.PARTY_MEMBER_LIMIT) {
				requestor.sendPacket(new SystemMessage(SystemMessageId.PARTY_FULL));// 队伍已满
				return;
			}
			if (target.isAutoAttack) {
				target.getAI().autoTeam(requestor, target);
				return;
			}
			requestor.onTransactionRequest(target);
			target.sendPacket(new AskJoinParty(requestor.getName(), requestor.getParty().getLootDistribution(), type));
			if (_log.isDebugEnabled())
				_log.debug("sent out a party invitation to:" + target.getName());

		} else {
			FuzePcInstance leader = requestor;// 队长
			party = leader.getParty();
			//如果对方的队伍解散了，则通知请求者那是个错误的目标
			if (party == null) {
				target.sendPacket(new SystemMessage(SystemMessageId.TARGET_IS_INCORRECT));
				return;
			}
			if (!party.isLeader(leader)) {
				leader = party.getLeader();
			}
			if (party.getMemberCount() >= L2Party.PARTY_MEMBER_LIMIT) {
				target.sendPacket(new SystemMessage(SystemMessageId.PARTY_FULL));// 队伍已满
				return;
			}

			// 队长是否离线
			if (!FuzeWorld.getInstance().getAllPlayers().containsKey(leader.getObjectId())) {
				msg = new SystemMessage(SystemMessageId.THE_TAGET_PARTY_LEADER_OFFLINE);
				target.sendPacket(msg);// 对方队长不在线，请稍后在尝试
				return;
			}
			if (!leader.getFriendInstance().getIsInvited()) {
				msg = new SystemMessage(SystemMessageId.TARGET_CLOSE_TEAM_INVITE);
				target.sendPacket(msg);// 对方关闭了组队邀请
				return;
			}
			if (leader.isAutoAttack) {
				leader.getAI().autoTeam(leader, target);
				return;
			}
			target.onTransactionRequest(leader);
			// in case a leader change has happened, use party's mode
			leader.sendPacket(new AskJoinParty(target.getName(), party.getLootDistribution(), type));// 请求加入leader的队伍
			if (_log.isDebugEnabled())
				_log.debug("sent out a party invitation to:" + target.getName());
		}
		msg = null;
	}

	@Override
	public String getType() {
		return _C__29_REQUESTJOINPARTY;
	}
}
